import dayjs from "dayjs";
import { testFn } from "../utils/common";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";

dayjs.extend(utc);
dayjs.extend(timezone);

function dayTest() {
  const d1 = dayjs("2024-01-20T23:08:29.759Z"); // 用utc格式时间创建day对象
  console.log("🚀 ~ testDay ~ d1:", d1);
  const d1str = d1.format("YYYY/MM/DD HH:mm:ss"); // 格式化为本地时间
  console.log("🚀 ~ dayTest ~ d1str:", d1str);
  const d2 = dayjs("2024-01-20 23:08:29"); // 用普通格式时间值创建day对象，会认为传入的是本地时间
  const d2local = d2.local();
  console.log("🚀 ~ dayTest ~ d2local:", d2local);
  console.log("🚀 ~ dayTest ~ d2:", d2);
  const dt = d1.toDate();
  console.log("🚀 ~ dayTest ~ dt:", dt);

  // current time zone is 'Asia/Shanghai' (offset +08:00)  , Europe/Paris offset +1
  // Parsing
  const tzDay1 = dayjs.tz("2013-11-18 11:55:20", "Europe/Paris");
  console.log("🚀 ~ dayTest ~ tzDay1:", tzDay1);
  const tzDay1LocalDateVal = tzDay1.toDate().toString(); // Pairs时区的时间 转换为本地时区的时间
  console.log("🚀 ~ dayTest ~ tzDay1LocalDateVal:", tzDay1LocalDateVal);

  const tzDay2 = dayjs("2013-11-18 11:55:20").tz("Europe/Paris"); // 用本地时间创建day对象，设置day对象的时区（指定day对象的当前时区，format时根据当前时区输出)

  console.log("🚀 ~ dayTest ~ tzDay2:", tzDay2.format("YYYY/MM/DD HH:mm:ss")); // 04:55:20

  const diff = d2.diff(d1, "day");
  console.log("🚀 ~ dayTest ~ diff:", diff);

  const timeZoneId = "Australia/Sydney"; // offset +11
  const dtVal = "2023-01-05T11:00:00+11:00";
  const localDay = dayjs(dtVal);
  console.log("🚀 ~ dayTest ~ localDay:", localDay);

  if (diff > 7) {
    console.log("expired already!");
  } else {
    console.log("token still available....");
  }

  const now = new Date();
  const nowDay = dayjs(now);
  console.log("🚀 ~ dayTest ~ nowDay:", nowDay);
  const todayStr = nowDay.format("YYYY/MM/DD");
  console.log("🚀 ~ dayTest ~ todayStr:", todayStr);

  const utcDate = dayjs.utc("2023-11-21 12:30:03");
  console.log("🚀 ~ dayTest ~ utcDate:", utcDate);
  const utcDate2 = dayjs.utc();
  console.log("🚀 ~ dayTest ~ utcDate2:", utcDate2);
}

export default testFn(dayTest);
